# 2、统计每个班级的平均年龄

# 1、读取学生表的数据
with open(file="../../data/student.txt", encoding="utf-8", mode="r") as f:
    lines = f.readlines()

# 2、统计每个班级总的年龄和总的人数
sum_age = {}
sum_num = {}
for line in lines:
    # 取出班级和年龄
    student = line.strip().split(",")
    # 年龄
    age = int(student[2])
    # 班级
    clazz = student[-1]

    # 累加统计总的年龄
    if clazz not in sum_age:
        sum_age[clazz] = age
    else:
        sum_age[clazz] += age

    # 统计总人数
    if clazz not in sum_num:
        sum_num[clazz] = 1
    else:
        sum_num[clazz] += 1

print(sum_age)
print(sum_num)

# 3、计算评平均年龄
clazz_avg_age = {}
for clazz, age in sum_age.items():
    # 使用班级到人数字典中获取班级的人数
    num = sum_num[clazz]

    # 使用总年龄/总人数
    avg_age = age / num

    # 将结果保存到字典中
    clazz_avg_age[clazz] = avg_age

# 4、保存结果
print(clazz_avg_age)